package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    private TravelCommentRepository travelCommentRepository;
    @Autowired
    private MongoTemplate template;


    @Override
    public void insert(TravelComment travelComment) {
        travelComment.setId(null);
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentRepository.deleteById(id);
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentRepository.save(travelComment);
    }

    @Override
    public TravelComment findById(String id) {
        Optional<TravelComment> travelComment = travelCommentRepository.findById(id);
        return travelComment.orElse(null);
    }

    @Override
    public List<TravelComment> findAll() {
        List<TravelComment> all = travelCommentRepository.findAll();
        return all;
    }

    @Override
    public Page query(TravelCommentQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getTravelId())){
            query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));
        }

        long count = template.count(query, TravelComment.class);
        if (count == 0){
            return Page.empty();
        }

        Pageable page = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),Sort.Direction.ASC,"createTime");
        query.with(page);
        List<TravelComment> list = template.find(query, TravelComment.class);

        return new PageImpl(list, page, count);
    }

    @Override
    public void addComment(TravelComment travelComment, UserInfo userInfo) {
        BeanUtils.copyProperties(userInfo, travelComment);
        travelComment.setUserId(userInfo.getId());
        travelComment.setCreateTime(new Date());
        if (travelComment.getType() == 1){
            travelComment.setRefComment(this.findById(travelComment.getRefComment().getId()));
        }
        this.insert(travelComment);
    }

    @Override
    public List<TravelComment> findByTravelId(String travelId) {
        List<TravelComment> travelComments = travelCommentRepository.findByTravelId(travelId);
        return travelComments;
    }


}
